package com.gitlab.lucky.auto.tld.phone;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @program: dp
 * @author: Mr.Han
 * @create: 2024-08-23 17:10
 **/
public class ZRPhone {
    private static final Logger logger = LoggerFactory.getLogger(ZRPhone.class);
    private static final String channelId = "1408858524640874510";
    public static String getPhoneNum(String token) throws IOException {
        Assert.notNull(token,"token is null");
        String url = "http://114.67.222.10:7000/api/jm_hqhm2/token=%s&itemid=%s&phonetype=0&Province=&phone=";
        String body = HttpUtil.get(String.format(url, token,"20566"));
        Assert.isTrue(StrUtil.isBlank(body));
        String[] split = body.split("\\|");
        Assert.isTrue("1".equals(split[0]));
        String phone = split[2];
        logger.info("获取手机号:[{}]",phone);
        return phone;
    }
    public static String getSMSCode(String token,String phone) throws InterruptedException, IOException {
        Assert.notNull(token,"token is null");
        Assert.notNull(phone,"phone is null");
        String url = "https://api.tqsms.xyz/api/getCode?token=%s&channelId=%s&phoneNum=%s";
        for (int i = 0; i < 15; i++) {
            logger.info("正在获取验证码:[{}]",i);
            Thread.sleep(3000L);
            String body = HttpUtil.get(String.format(url, token, channelId, phone));
            JSONObject jsonObject = JSONUtil.parseObj(body);
            if("200".equals(jsonObject.getStr("status"))){
                JSONObject data = jsonObject.getJSONObject("data");
                String code = data.getStr("code");
                if(StrUtil.isBlank(code)){
                    continue;
                }
                logger.info("获取验证码:[{}]",body);
                return code;
            }
        }
        return null;
    }
    public static String getToken() throws IOException {
        String body = HttpUtil.get("http://114.67.222.10:7000/api/sign/username=hanxu1216&password=hanxu1216");
        Assert.isTrue(StrUtil.isNotBlank(body));
        String[] split = body.split("\\|");
        Assert.isTrue("1".equals(split[0]));
        return split[1];
    }

    public static void main(String[] args) throws IOException {
        String token = getToken();
        String phoneNum = getPhoneNum(token);
        System.out.println(token);
    }
}
